/*  Copyright (C) 2012 Philip Smith
 *
 *  This file is part of SuperChat.
 *
 *  SuperChat is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SuperChat is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with SuperChat.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.supertycoon.bukkit.superchat;

import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class SuperChat extends JavaPlugin {

	static Server serverInstance;
	static SuperChat pluginInstance;
	static Logger logger;
	static HashMap<String, ChatRoom> rooms;
	static HashMap<CommandSender, PlayerShit> players;
	static HashSet<CommandSender> mainIgnorers;
	static HashSet<CommandSender> monitors;
	static HashSet<CommandSender> toggled;

	@Override
	public void onLoad() {
		serverInstance = this.getServer();
		pluginInstance = this;
		logger = this.getLogger();
	}

	@Override
	public void onEnable() {
		rooms = new HashMap<String, ChatRoom>();
		players = new HashMap<CommandSender, PlayerShit>();
		mainIgnorers = new HashSet<CommandSender>();
		monitors = new HashSet<CommandSender>();
		toggled = new HashSet<CommandSender>();
		this.getServer().getPluginManager().registerEvents(new SuperChatListener(), pluginInstance);
		this.getCommand("c").setExecutor(new cCommandHandler());
		this.getCommand("chat").setExecutor(new chatCommandHandler());
	}

	@Override
	public void onDisable() {
		for (ChatRoom room : rooms.values()) {
			room.chat(ChatColor.RED.toString() + "SuperChat is being shutdown, chat room will be closed");
		}
	}

	static void onPlayerChatHigh(PlayerChatEvent event) {
		for (CommandSender player : mainIgnorers) {
			event.getRecipients().remove(player);
		}
	}
	
	static void onPlayerChatLow(PlayerChatEvent event) {
		if (SuperChat.toggled.contains(event.getPlayer())) {
			SuperChat.players.get(event.getPlayer()).toggledChat(event.getMessage());
			event.setCancelled(true);
			return;
		}
		if (SuperChat.players.containsKey(event.getPlayer()) && SuperChat.players.get(event.getPlayer()).isIgnoringMain
				&& !event.getPlayer().hasPermission("superchat.mod.chatwojoin")) {
			event.getPlayer().sendMessage(ChatColor.RED.toString() + "You are not in the main chat, you cannot send messages there");
			event.setCancelled(true);
			return;
		}
	}
	
	static void onPlayerQuit(PlayerQuitEvent event) {
		if (SuperChat.players.containsKey(event.getPlayer())) {
			SuperChat.players.get(event.getPlayer()).quit();
			SuperChat.players.remove(event.getPlayer());
		}
	}
	
	static String concat(String[] input, String delimiter) {
		if (input.length == 0)
			return "";
		StringBuilder temp = new StringBuilder("");
		for (int i = 0; i < input.length - 1; i++) {
			temp.append(input[i]);
			temp.append(delimiter);
		}
		temp.append(input[input.length-1]);
		return temp.toString();
	}
	
}
